package com.edgar.fabric;

import com.supermap.blockchain.fabricsdk.SmChain;
import org.junit.Assert;

import java.io.File;
import java.util.UUID;

/**
 * @author liqs
 * @version 1.0
 * @date 2021/6/16 16:58
 */
public class FabricClient {

    // Fabric
    private String channelName;
    private String chaincodeName;
    private File networkFile;
    private SmChain smChain;

    public FabricClient(String channelName, String chaincodeName, String networkFilePath) {
        this.channelName = channelName;
        this.chaincodeName = chaincodeName;
        this.networkFile = new File(networkFilePath);
        smChain = SmChain.getChain(this.channelName, this.networkFile);
    }

    public FabricClient(String channelName, String chaincodeName, File networkFile) {
        this.channelName = channelName;
        this.chaincodeName = chaincodeName;
        this.networkFile = networkFile;
        smChain = SmChain.getChain(this.channelName, this.networkFile);
    }

    public void invoke() {
        String key = UUID.randomUUID().toString();
        String result = smChain.getTransaction().invokeByString
                (
                chaincodeName,
                "PutStringRecordByKey",
                new String[]{key, key}
                );
        Assert.assertEquals(64, result.length());
        String string = smChain.getTransaction().queryByString(
                chaincodeName,
                "GetStringRecordByKey",
                new String[]{key}
        );
        Assert.assertEquals(key, key);
    }
}
